package com.example.liliangwei.topnewslearn.fragment;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;

import com.example.liliangwei.topnewslearn.adapter.NewsAdapter;
import com.example.liliangwei.topnewslearn.bean.NewsEntity;
import com.example.liliangwei.topnewslearn.tool.Constants;
import com.example.liliangwei.topnewslearn.R;

import java.util.ArrayList;

import android.os.Handler;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Created by Administrator on 2015/12/2.
 */
public class NewsFragment extends Fragment {
    private String text;
    private ArrayList<NewsEntity> newsList = new ArrayList<NewsEntity>();
    private Activity activity;
    public final static int SET_NEWSLIST = 0;
    ImageView detail_loading;

    private NewsAdapter mAdapter;
    private ListView mListView;
    Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
                case SET_NEWSLIST:
                    detail_loading.setVisibility(View.GONE);
                    mAdapter = new NewsAdapter(activity, newsList);
                    mListView.setAdapter(mAdapter);
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Bundle args = getArguments();
        text = args != null ? args.getString("text") : "";
        initData();
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        this.activity = activity;
        super.onAttach(activity);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser){
            if (newsList != null && newsList.size() != 0){
                handler.obtainMessage(SET_NEWSLIST).sendToTarget();
            }else {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            Thread.sleep(2);
                        }catch (InterruptedException e){
                            e.printStackTrace();
                        }
                        handler.obtainMessage(SET_NEWSLIST).sendToTarget();
                    }

                }).start();
            }
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    private void initData() {
        newsList = Constants.getNewsList();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.news_fragment,null);
        mListView = (ListView) view.findViewById(R.id.mListView);
        TextView itemTextView = (TextView) view.findViewById(R.id.item_textview);
        detail_loading = (ImageView) view.findViewById(R.id.detail_loading);
        itemTextView.setText(text);
        return view;
    }
}
